2 Building Static Maps in R Studio
2.1 Load your libraries and set your working directory
# Load libraries
library(tmap)
library(sf)
library(tidyverse)If you don’t already have the packages installed, you must install them before loading the libraries; you can install the libraries with the following:
install.packages(c("tmap", "sf", "tidyverse"))After your libraries are successfully installed and loaded, set your working directory by passing the file path of the directory that contains your data to the setwd function, as in the example below (but note your file path will look different).
# Set your working directory to the directory containing your data
setwd("/Users/adra7980/Documents/git_repositories/gistools_qda/data")If you don’t know the relevant file path, you can set your working directory manually by opening the Session menu, scrolling down to
2.2 Read in a spatial dataset
# Read in the shapefile named "usa_shapefile.shp"
usa_shapefile<-st_read("usa_shapefile.shp")## Reading layer `usa_shapefile' from data source `/Users/adra7980/Documents/git_repositories/gistools_qda/data/usa_shapefile.shp' using driver `ESRI Shapefile'
## Simple feature collection with 52 features and 3 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -4088650 ymin: -1696697 xmax: 2258200 ymax: 1565782
## projected CRS: USA_Contiguous_Albers_Equal_Area_Conic
2.3 View the spatial dataset’s attribute table
usa_shapefile## Simple feature collection with 52 features and 3 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -4088650 ymin: -1696697 xmax: 2258200 ymax: 1565782
## projected CRS: USA_Contiguous_Albers_Equal_Area_Conic
## First 10 features:
## GEOID name abbrev geometry
## 1 23 Maine ME MULTIPOLYGON (((2217413 111...
## 2 25 Massachusetts MA MULTIPOLYGON (((2057879 734...
## 3 26 Michigan MI MULTIPOLYGON (((548934.7 12...
## 4 30 Montana MT MULTIPOLYGON (((-633753 865...
## 5 32 Nevada NV MULTIPOLYGON (((-1581791 94...
## 6 34 New Jersey NJ MULTIPOLYGON (((1727369 428...
## 7 36 New York NY MULTIPOLYGON (((1977956 675...
## 8 37 North Carolina NC MULTIPOLYGON (((1192302 -79...
## 9 39 Ohio OH MULTIPOLYGON (((1085606 548...
## 10 42 Pennsylvania PA MULTIPOLYGON (((1733137 446...
2.4 Map the spatial dataset
tm_shape(usa_shapefile)+
tm_polygons()usa_map<-tm_shape(usa_shapefile)+
tm_polygons()usa_map2.5 Read in a tabular dataset with the information to map
usa_trifecta_data<-read_csv("usa_trifecta.csv")##
## ── Column specification ─────────────────────────────────────────────────────────────────────────────────────────
## cols(
## GEOID = col_double(),
## NAME = col_character(),
## abbrev = col_character(),
## Composition = col_character()
## )
usa_trifecta_data## # A tibble: 50 x 4
## GEOID NAME abbrev Composition
## <dbl> <chr> <chr> <chr>
## 1 1 Alabama AL Republican Trifecta
## 2 2 Alaska AK Divided Government
## 3 4 Arizona AZ Republican Trifecta
## 4 5 Arkansas AR Republican Trifecta
## 5 6 California CA Democratic Trifecta
## 6 8 Colorado CO Democratic Trifecta
## 7 9 Connecticut CT Democratic Trifecta
## 8 10 Delaware DE Democratic Trifecta
## 9 12 Florida FL Republican Trifecta
## 10 13 Georgia GA Republican Trifecta
## # … with 40 more rows
2.6 Load Tabular data
usa_trifecta<-read_csv("usa_trifecta.csv")##
## ── Column specification ─────────────────────────────────────────────────────────────────────────────────────────
## cols(
## GEOID = col_double(),
## NAME = col_character(),
## abbrev = col_character(),
## Composition = col_character()
## )
usa_trifecta## # A tibble: 50 x 4
## GEOID NAME abbrev Composition
## <dbl> <chr> <chr> <chr>
## 1 1 Alabama AL Republican Trifecta
## 2 2 Alaska AK Divided Government
## 3 4 Arizona AZ Republican Trifecta
## 4 5 Arkansas AR Republican Trifecta
## 5 6 California CA Democratic Trifecta
## 6 8 Colorado CO Democratic Trifecta
## 7 9 Connecticut CT Democratic Trifecta
## 8 10 Delaware DE Democratic Trifecta
## 9 12 Florida FL Republican Trifecta
## 10 13 Georgia GA Republican Trifecta
## # … with 40 more rows
2.7 Merge data
usa_shapefile_trifecta<-right_join(usa_shapefile, usa_trifecta, by="abbrev" )usa_shapefile_trifecta## Simple feature collection with 50 features and 6 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -4088650 ymin: -1696697 xmax: 2258200 ymax: 1565782
## projected CRS: USA_Contiguous_Albers_Equal_Area_Conic
## First 10 features:
## GEOID.x name abbrev GEOID.y NAME Composition geometry
## 1 23 Maine ME 23 Maine Democratic Trifecta MULTIPOLYGON (((2217413 111...
## 2 25 Massachusetts MA 25 Massachusetts Divided Government MULTIPOLYGON (((2057879 734...
## 3 26 Michigan MI 26 Michigan Divided Government MULTIPOLYGON (((548934.7 12...
## 4 30 Montana MT 30 Montana Republican Trifecta MULTIPOLYGON (((-633753 865...
## 5 32 Nevada NV 32 Nevada Democratic Trifecta MULTIPOLYGON (((-1581791 94...
## 6 34 New Jersey NJ 34 New Jersey Democratic Trifecta MULTIPOLYGON (((1727369 428...
## 7 36 New York NY 36 New York Democratic Trifecta MULTIPOLYGON (((1977956 675...
## 8 37 North Carolina NC 37 North Carolina Divided Government MULTIPOLYGON (((1192302 -79...
## 9 39 Ohio OH 39 Ohio Republican Trifecta MULTIPOLYGON (((1085606 548...
## 10 42 Pennsylvania PA 42 Pennsylvania Divided Government MULTIPOLYGON (((1733137 446...
2.8 Map the trifecta data
Start with a rough map:
usa_trifecta_map<-tm_shape(usa_shapefile_trifecta)+
tm_polygons(col="Composition")
usa_trifecta_mapAdjust the color scheme, such that it is red for a Republican trifecta, dark blue for a democratic trifecta, and
# Sets composition variable as factor
usa_shapefile_trifecta<-usa_shapefile_trifecta %>%
mutate(Composition=factor(Composition, levels=c("Democratic Trifecta", "Republican Trifecta", "Divided Government")))# Maps data with custom colors; note the changed order in the legend
colors<-c("navy", "red3", "seashell")
usa_trifecta_map<-tm_shape(usa_shapefile_trifecta)+
tm_polygons(col="Composition",
palette=colors)
usa_trifecta_mapNow, let’s shift the legend, remove the legend title, and add a title for the map:
usa_trifecta_map<-usa_trifecta_map<-tm_shape(usa_shapefile_trifecta)+
tm_polygons(col="Composition",
palette=colors,
title="")+
tm_layout(legend.position = c("left", "center"),
frame=FALSE,
main.title="Partisan Composition of State Governments, 2022",
main.title.size=1,
main.title.position="center")
usa_trifecta_map## legend.postion is used for plot mode. Use view.legend.position in tm_view to set the legend position in view mode.